package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @author jay
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("shoppingCart：{}", shoppingCart);
        //设置用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //查询加入的菜品或套餐是否在购物车中，若不在则加入（菜品需在判断口味是否相同）
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);//userId过滤
        //判断时加入的是菜品还是套餐
        if (shoppingCart.getSetmealId() != null) {
            //是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
            //判断是否存在
            if (shoppingCart1 != null) {
                //已存在
                Integer number = shoppingCart1.getNumber();
                shoppingCart1.setNumber(number + 1);
                shoppingCartService.updateById(shoppingCart1);
                shoppingCart = shoppingCart1;
            } else {
                //不存在
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartService.save(shoppingCart);
            }
        } else {
            //是菜品
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
            //判断是否存在
            if (shoppingCart1 != null) {
                //已存在
                Integer number = shoppingCart1.getNumber();
                shoppingCart1.setNumber(number + 1);
                shoppingCartService.updateById(shoppingCart1);
                shoppingCart = shoppingCart1;
            } else {
                //不存在
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartService.save(shoppingCart);
            }
        }
        return R.success(shoppingCart);

    }

    /**
     * 购物车列表查询
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //拿到用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId != null,ShoppingCart::getUserId, userId);
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);//排序
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 购物车菜品或套餐-1
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //拿到用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId != null,ShoppingCart::getUserId, userId);
        //判断时加入的是菜品还是套餐
        if (shoppingCart.getSetmealId() != null) {
            //是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
            //判断数量是否为1
            if (shoppingCart1.getNumber() == 1) {
                //删除
                shoppingCartService.remove(queryWrapper);
            } else {
                //Number--
                int number = shoppingCart1.getNumber();
                shoppingCart1.setNumber(number - 1);
                shoppingCartService.updateById(shoppingCart1);
            }
        } else {
            //是菜品
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
            //判断数量是否为1
            if (shoppingCart1.getNumber() == 1) {
                //删除
                shoppingCartService.remove(queryWrapper);
            } else {
                //Number--
                int number = shoppingCart1.getNumber();
                shoppingCart1.setNumber(number - 1);
                shoppingCartService.updateById(shoppingCart1);
            }
        }
        return R.success(shoppingCart);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> delete(){
        //拿到用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId != null,ShoppingCart::getUserId, userId);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车完成");
    }
}
